#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
    pid_t id1=fork();
    if(id1==0)
    {
        //可变参数列表,完整路径
        execl("/bin/ls","ls","-a","-l",NULL);
        perror("execl");
        exit(1);
    }
    else if(id1>0)
    {
        pid_t pid1=wait(NULL);
        if(pid1>0)
        printf("等待子进程成功\n");
        else 
        printf("等待子进程失败\n");
    }
    pid_t id2=fork();
    //一定要注意这种格式,否则C++11不支持
    char *const args[]={(char*)"ls",(char*)"-l",(char*)"-a",NULL};
    if(id2==0)
    {
        //使用参数数组,完整路径
        execv("/bin/ls",args);
        perror("execv");
        exit(1);
    }
    else if(id2>0)
    {
        pid_t pid2=wait(NULL);
        if(pid2>0)
        printf("等待子进程成功\n");
        else 
        printf("等待子进程失败\n");
    }
    pid_t id3=fork();
    if(id3==0)
    {
        //可变参数列表,在path路径下查找指令
        execlp("ls","ls","-l","-a",NULL);
        perror("execlp");
        exit(1);
    }
    else if(id3>0)
    {
        pid_t pid3=wait(NULL);
        if(pid3>0)
        printf("等待子进程成功\n");
        else 
        printf("等待子进程失败\n");
    }
    //同理execvp
    //参数数组,path路径,传环境变量
    char *const env[]={
        (char*)"HELLO=my",
        (char*)"HELLO1=your",
        (char*)"HELLO2=his" 
    };
    pid_t id4=fork();
    if(id4==0)
    {
        execvpe("ls",args,env);
        perror("execvpe");
        exit(1);
    }
    else if(id4>0)
    {
        printf("子进程执行成功\n");
    }
    return 0;
}
